package com.goodboys.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.goodboys.entity.ArticleBean;
import com.goodboys.entity.MailBean;
import org.jsoup.Connection;

import java.io.IOException;
import java.util.List;

/**
 * Created by CJ on 2018-07-21.
 */
public class GoldMantisUtil {
    /**
     * 根据工号阅读文章
     * @param jobNumber 用户工号
     */
    public static void readArticle(String jobNumber){

        try {
            String html = JsoupUtils.getHtml("http://hr.goldmantis.com/hr/wechat/wechat/getByReadList.jtl?jobNumber="+jobNumber+"&isRead=0&selectDate=2018-7-19");
            List<ArticleBean> articleBeans = JSONObject.parseArray(html, ArticleBean.class);
            articleBeans.stream().forEach(bean -> {
                //查看文章
                try {
                    JsoupUtils.getHtml("http://hr.goldmantis.com/hr/wechat/wechat/getByMsgId.jtl?jobNumber="+jobNumber+"&msgId=" + bean.getId());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 阅读邮件
     * @throws IOException
     */
    public static void readMail(String account,String pwd) throws IOException {
        //获取邮件列表
        Connection conn = JsoupUtils.getConnection("http://erp2.goldmantis.com//api/HomeApi/GetMailInfos");
        //获取用户token,并设置请求头
        String token = getUserToken(account, pwd);
        conn.header("Authorization","Basic "+token);
        String json = conn.ignoreContentType(true).execute().body();
        //将json转化为对象
        List<MailBean> mailBeans = JSONObject.parseArray(JSON.parseObject(json).getString("mails"), MailBean.class);
        mailBeans.stream().forEach(bean -> {
            //阅读没有看的邮件
            if(bean != null && bean.getIsUnRead()){
                String url  ="http://erp2.goldmantis.com/Home/EmailDetail?accessToken="+ token + "&" + bean.getLink();
                try {
                    String html = JsoupUtils.getHtml(url);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }



    public static String getUserToken(String account,String pwd) {
        String token = null;
        try {
            String body = JsoupUtils.getConnection("http://erp2.goldmantis.com/api/HomeApi/LogOn")
                    .method(Connection.Method.POST)
                    .ignoreContentType(true)
                    .data("Account", account)
                    .data("Password", pwd).execute().body();
//            System.out.println(body);
            //获取用户token值
            JSONObject jsonObject = JSON.parseObject(body);
            token = jsonObject.getString("Token");
            //用户其他信息暂时不读去；后期需要可在这里获取
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    /**
     * 获取用户的邮件地址（地址的sid会动态更新）目前未使用
     * @param token
     */
    public static String getUserMailLink(String token){
        try {
            String mailLink = JsoupUtils.getConnection("http://erp2.goldmantis.com//api/HomeApi/GetUserMailLink")
                    .header("Authorization", token)
                    .ignoreContentType(true)
                    .execute().body().replaceAll("\"","");
            return mailLink;
        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }

    }
}
